<?php

namespace App\Http\Controllers\Api;

use App\Http\Requests\Api\ImageRequest;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class ImagesController extends Controller
{

    public function store(ImageRequest $request)
    {
        $originalName = $request->image->getClientOriginalName();
//        if (preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $originalName) > 0) {
//            $originalName = \chinese2pinyin::encode($originalName);
//        } elseif (preg_match('/[\x{4e00}-\x{9fa5}]/u', $originalName) > 0) {
//            $originalName = \chinese2pinyin::encode($originalName);
//        }
//        echo md5($originalName) . "." . $request->image->getClientOriginalExtension();exit;

        $path = $request->image->storeAs('',md5($originalName) . "." . $request->image->getClientOriginalExtension());
        //storage_path返回根目录下的storage的绝对路径 里面放的直接丢在后面
//        $filePath = storage_path('app/' . $path);
//        echo base_path();exit;
//        echo $path;exit;
        //上传到七牛
//        $ret = qiniu_upload($filePath);  //调用的全局函数
//        Storage::move($path,"../../mb.jpg");
//        print_r($ret);
//        exit;

        return $this->response->array([
            'url' => "http://".$request->server('HTTP_HOST')."/uploads/" . $path
        ]);
    }

}
